<template>
  <header class="navbar">
    <!-- 左侧 Logo 和标题 -->
    <div class="navbar-left">
      <div class="logo">
        <div class="logo-icon">V</div>
        <span class="logo-title">{{ title }}</span>
      </div>
    </div>

    <!-- 中间导航链接 -->
    <nav class="navbar-center">
      <ul class="nav-links">
        <li v-for="(item, index) in navLinks" :key="index">
          <a 
            :href="item.path" 
            class="nav-link" 
            :class="{ active: item.active }"
            @click.prevent="handleNavClick(item)"
          >
            {{ item.text }}
          </a>
        </li>
      </ul>
    </nav>

    <!-- 右侧用户菜单 -->
    <div class="navbar-right">
      <div class="user-menu" @click="toggleUserMenu">
        <div class="user-avatar">
          {{ userInfo.avatar }}
        </div>
        <span class="user-name">{{ userInfo.name }}</span>
        <span class="menu-arrow" :class="{ rotated: showUserMenu }">▼</span>
        
        <!-- 用户下拉菜单 -->
        <div class="user-dropdown" v-show="showUserMenu">
          <div 
            v-for="(item, index) in userMenuItems" 
            :key="index" 
            class="dropdown-item"
            @click="handleUserMenuItemClick(item)"
          >
            {{ item.text }}
          </div>
        </div>
      </div>
    </div>
  </header>
</template>

<script>
export default {
  name: 'NavBar',
  props: {
    // 标题
    title: {
      type: String,
      default: 'Vue应用'
    },
    // 导航链接列表
    navLinks: {
      type: Array,
      default: () => [
        { text: '首页', path: '/', active: true },
        { text: '产品', path: '/products', active: false },
        { text: '关于', path: '/about', active: false },
        { text: '联系我们', path: '/contact', active: false }
      ]
    },
    // 用户信息
    userInfo: {
      type: Object,
      default: () => ({
        name: '用户',
        avatar: 'U'
      })
    },
    // 用户菜单列表
    userMenuItems: {
      type: Array,
      default: () => [
        { text: '个人中心', action: 'profile' },
        { text: '设置', action: 'settings' },
        { text: '退出登录', action: 'logout' }
      ]
    }
  },
  data() {
    return {
      showUserMenu: false
    }
  },
  mounted() {
    // 点击外部关闭用户菜单
    document.addEventListener('click', this.handleClickOutside)
  },
  beforeUnmount() {
    document.removeEventListener('click', this.handleClickOutside)
  },
  methods: {
    // 切换用户菜单显示状态
    toggleUserMenu(event) {
      event.stopPropagation()
      this.showUserMenu = !this.showUserMenu
    },
    // 处理导航链接点击
    handleNavClick(item) {
      // 更新激活状态
      const updatedLinks = this.navLinks.map(link => ({
        ...link,
        active: link.path === item.path
      }))
      // 触发自定义事件，通知父组件更新导航链接状态
      this.$emit('nav-update', updatedLinks)
      // 触发导航事件
      this.$emit('nav-click', item)
    },
    // 处理用户菜单项点击
    handleUserMenuItemClick(item) {
      this.showUserMenu = false
      this.$emit('user-menu-click', item)
    },
    // 点击外部关闭菜单
    handleClickOutside(event) {
      if (!this.$el.contains(event.target)) {
        this.showUserMenu = false
      }
    }
  }
}
</script>

<style scoped>
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  height: 60px;
  padding: 0 20px;
  position: relative;
}

/* 左侧 Logo 和标题 */
.navbar-left {
  display: flex;
  align-items: center;
}

.logo {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.logo-icon {
  width: 40px;
  height: 40px;
  background-color: #409eff;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: bold;
  margin-right: 10px;
}

.logo-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

/* 中间导航链接 */
.navbar-center {
  flex: 1;
  max-width: 600px;
  margin: 0 auto;
}

.nav-links {
  display: flex;
  list-style: none;
  padding: 0;
  margin: 0;
  justify-content: center;
}

.nav-links li {
  margin: 0 15px;
}

.nav-link {
  color: #606266;
  text-decoration: none;
  padding: 5px 10px;
  border-radius: 4px;
  transition: all 0.3s ease;
  font-size: 14px;
}

.nav-link:hover {
  color: #409eff;
}

.nav-link.active {
  color: #409eff;
  font-weight: 500;
  border-bottom: 2px solid #409eff;
}

/* 右侧用户菜单 */
.navbar-right {
  position: relative;
}

.user-menu {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.3s ease;
  position: relative;
}

.user-menu:hover {
  background-color: #f5f7fa;
}

.user-avatar {
  width: 32px;
  height: 32px;
  background-color: #e6a23c;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 500;
  margin-right: 8px;
}

.user-name {
  color: #606266;
  font-size: 14px;
  margin-right: 5px;
}

.menu-arrow {
  font-size: 10px;
  color: #909399;
  transition: transform 0.3s ease;
}

.menu-arrow.rotated {
  transform: rotate(180deg);
}

/* 用户下拉菜单 */
.user-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  background-color: white;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  border-radius: 4px;
  min-width: 150px;
  margin-top: 5px;
  z-index: 1000;
}

.dropdown-item {
  padding: 10px 15px;
  color: #606266;
  font-size: 14px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.dropdown-item:hover {
  background-color: #f5f7fa;
  color: #409eff;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .navbar {
    padding: 0 15px;
  }
  
  .logo-title {
    display: none;
  }
  
  .nav-links li {
    margin: 0 8px;
  }
  
  .user-name {
    display: none;
  }
}
</style>